package com.hs.hlw.credit.system.business.service.impl;

import com.hs.hlw.credit.system.business.dto.ResponseDto;
import com.hs.hlw.credit.system.business.service.RegularExpressionService;
import com.hs.hlw.credit.system.data.mybatis.Pageable;
import com.hs.hlw.credit.system.data.primary.mapper.RegularExpressionMapper;
import com.hs.hlw.credit.system.data.primary.model.RegularExpression;
import com.hualongdata.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * RegularExpressionServiceImpl
 * @author zhb
 * @date 2018/11/19
 */
@Service
public class RegularExpressionServiceImpl implements RegularExpressionService {
    private @Autowired RegularExpressionMapper regularExpressionMapper;

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    public ResponseDto page(Pageable pageable) {
        List<RegularExpression> list = regularExpressionMapper.page(pageable);
        return new ResponseDto("查询成功", pageable, list);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public ResponseDto detail(Integer id) {
        RegularExpression dto = regularExpressionMapper.selectByPrimaryKey(id);
        if (dto == null) {
            throw new BusinessException("记录不存在");
        }
        return new ResponseDto("查询成功", dto);
    }

    /**
     * 新增
     * @param regularExpression
     * @return
     */
    public ResponseDto add(RegularExpression regularExpression) {
        regularExpression.setCreateTime(new Date());
        regularExpressionMapper.insertSelective(regularExpression);
        return new ResponseDto("操作成功");
    }

    /**
     * 修改
     * @param regularExpression
     * @return
     */
    public ResponseDto update(RegularExpression regularExpression) {
        regularExpression.setUpdateTime(new Date());
        regularExpressionMapper.updateByPrimaryKeySelective(regularExpression);
        return new ResponseDto("操作成功");
    }

    /**
     * 批量修改状态
     * @param list
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDto batchUpdateStatus(List<RegularExpression> list) {
        Date time = new Date();
        for (RegularExpression dto : list) {
            dto.setUpdateTime(time);
            regularExpressionMapper.updateByPrimaryKeySelective(dto);
        }
        return new ResponseDto("操作成功");
    }
}